package com.wyh.service.serviceImpl.appservieripml;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.dto.groupfour.customerDTO.ContactsDTO;
import com.wyh.domain.po.gfour.Contacts;
import com.wyh.domain.vo.gruopfour.customerVO.ContactsVO;
import com.wyh.mapper.appmapper.ContactsMapper;
import com.wyh.service.appservice.ContactsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
@Slf4j
public class ContactsServiceImpl implements ContactsService {


    @Autowired
    private ContactsMapper contactsMapper;

    public ContactsServiceImpl(ContactsMapper contactsMapper) {
        this.contactsMapper = contactsMapper;
    }

    @Override
    public List<ContactsVO> findContactsByCustomerId(int customerId, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        Page<ContactsVO> page = (Page<ContactsVO>) contactsMapper.selectContactsByCustomerId(customerId);
        log.info("page:{}", page.getResult());
        return page.getResult();
    }

    @Override
    public void deleteContacts(Integer id) {
        this.contactsMapper.deleteContactsById(id);
    }

    @Override
    @Transactional
    public Integer addContacts(ContactsDTO contactsDTO) {
        Contacts contactsMapperByPhone = contactsMapper.findByPhone(contactsDTO.getTelephone());
        Contacts contacts = new Contacts();
        if (contactsMapperByPhone == null){
            return add(contactsDTO, contacts);
        }else {
            return update(contactsDTO, contactsMapperByPhone);
        }
    }

    private Integer update(ContactsDTO contactsDTO, Contacts contacts) {
        String s = contactsDTO.getCustomerId().toString().replace("[", "").replace("]", "");
        Set<String> set1 = Arrays.stream(contacts.getCustomerid().split(",")).collect(Collectors.toSet());
        Set<String> set2 = Arrays.stream(s.split(",")).collect(Collectors.toSet());
        Set<String> combinedSet = new HashSet<>(set1);
        combinedSet.addAll(set2);

        // 将合并后的Set转换回字符串
        String customerids = String.join(",", combinedSet);
        contacts.setCustomerid(customerids);
        return contactsMapper.updateContacts(contacts);
    }

    @Transactional
    public Integer add(ContactsDTO contactsDTO, Contacts contacts) {
        try {
            contactsDTO.setRole(1);
            contacts.setTelephone(contactsDTO.getTelephone());
            contacts.setName(this.generateName(contactsDTO.getTelephone()));
            String customerid = contactsDTO.getCustomerId().toString().replace("[", "").replace("]", "");
            contacts.setCustomerid(customerid);
            contacts.setActivetime(contactsDTO.getActivetime());
            contacts.setRole(contactsDTO.getRole());
            this.contactsMapper.addContacts(contacts);

            contactsMapper.addUser(contacts.getTelephone());

            Integer userId = contactsMapper.getUserByPhone(contacts.getTelephone());

            contactsMapper.addUserRole(userId);

            return 1;
        } catch (Exception e) {
            throw new CustomException(ResultCodeEnum.SYSTEM_ERROR.getCode(), "添加失败");
        }
    }


    private String generateName(String telephone) {
        String name = telephone.substring(0, 3) + "*****" + telephone.substring(telephone.length() - 4);
        return name;
    }
}
